﻿#include"Shop.h"
#include<cstring>
#include<iostream>
#include<map>

Shop::Shop()
{
    std::cout<<"000000000000\n";
    Init();
}

void Shop::Init()
{
    std::cout<<"1\n";
    m_goods.clear();std::cout<<"2\n";
    std::string name;std::cout<<"3\n";
    float price;std::cout<<"4\n";
    std::string prop;std::cout<<"5\n";
    int changeVal;std::cout<<"16\n";
    shopFile.open("shopInfo.txt");std::cout<<"7\n";
    if (shopFile.is_open())
    {
        std::cout<<"Shop Info Initing...";
        char line[128];
        do
        {
            shopFile.getline(line,128);
            shopStr+=line;
            name=strtok(line,":");
            price=atof(strtok(NULL,","));
            prop=strtok(NULL,",");
            changeVal=atoi(strtok(NULL,";"));
            Good newGood=Good(name,price,prop,changeVal);
            AddGood(newGood);
        } while(strlen(line)>0);
        
        shopFile>>shopStr;
        std::cout<<"ShopStr"<<shopStr<<std::endl;
        shopFile.close();
    }    
}

int Shop::AddGood(Good newGood){    
    if(m_goods.find(newGood.name)==m_goods.end()){
         m_goods.insert(std::map<std::string,Good>::value_type(newGood.name,newGood));       
         return 0;
    }
    return 1;
}

int Shop::DelGood(Good delGood){
    if(m_goods.find(delGood.name)==m_goods.end()){
        return 1;//物品不存在
    }
    m_goods.erase(delGood.name);
    return 0;

}

Shop::~Shop()
{
    m_goods.clear();
}

Good::Good(std::string name,float price,std::string prop,int changeVal)
{
    this->name=name;
    this->changeVal=changeVal;
    this->price=price;
    this->prop=prop;
}

Good::Good()
{
    this->name="";
    this->changeVal=0;
    this->price=0;
    this->prop="";
}


